﻿Axis2D = function () {
    Sim.Object.call(this);
};

Axis2D.prototype = new Sim.Object();
var xOpacity, yOpacity, zOpacity, scaleSize;
Axis2D.prototype.init = function (groupPoints,maxV, minV, step, effectController, xyz,geometries) {
    var group = new THREE.Object3D();
    this.setObject3D(group);
    xOpacity = effectController.X坐标透明度;
    yOpacity = effectController.Y坐标透明度;
    zOpacity = effectController.Z坐标透明度;
    scaleSize = effectController.坐标字体;

    maxV.divideScalar(step).floor().multiplyScalar(step).addScalar(step);
    minV.divideScalar(step).floor().multiplyScalar(step);

    this.geometries = geometries;


    var trackingLine = this.createTrackingLine(groupPoints, xyz);
    if (xyz.equals(new THREE.Vector3(1,1,0))) {
        var face = this.createXYFace(minV, maxV, step);
        group.add(face);
    }

    if (xyz.equals(new THREE.Vector3(1, 0, 1))) {
        face = this.createXZFace(minV, maxV, step);
        group.add(face);
        face.rotateX(Math.PI / 2);
        trackingLine.rotateX(Math.PI / 2);
    }

    if (xyz.equals(new THREE.Vector3(0, 1, 1))) {
        face = this.createYZFace(minV, maxV, step);
        group.add(face);
        face.rotateY(-Math.PI / 2);
        trackingLine.rotateY(-Math.PI / 2);
    }


    group.add(trackingLine);
};

Axis2D.prototype.createTrackingLine = function(groupPoints, xyz) {
    var geometry = new THREE.SphereGeometry(effectController.半径);
    var material = new THREE.MeshPhongMaterial({ color: 0xff0000 });
  
    var group = new THREE.Object3D();
    for (key in groupPoints) {
        var list = groupPoints[key];
        if (!groupPoints[key].length) {
            continue;
        }
        var vectors = [];

        for (var i = 0; i < list.length; i++) {
            var spMesh = new THREE.Mesh(geometry, material);
            var vecotr = new THREE.Vector3(list[i].Vector3.x, list[i].Vector3.y, list[i].Vector3.z).multiply(xyz);
            spMesh.position = vecotr;
            group.add(spMesh);
            vectors.push(vecotr);
        }

        var randomSpline = new THREE.SplineCurve3(vectors);
        var tube = new THREE.TubeGeometry(randomSpline, 32, 0.1, 10, false);

        var mesh = new THREE.Mesh(tube, material);
        group.add(mesh);
    }
    return group;
};

Axis2D.prototype.createXYFace = function (minV, maxV, step) {
    var face = new THREE.Object3D();

    var x = this.createX(minV, maxV, step);
    var y = this.createY(minV, maxV, step, this.rotateYText, Math.PI / 4);
//    var xy = this.createXY(minV, maxV, step);
    face.add(x);
    face.add(y);
//    face.add(xy);
    return face;
};

Axis2D.prototype.createXZFace = function (minV, maxV, step) {
    var face = new THREE.Object3D();

    var x = this.createX(minV, maxV, step ,this.rotateXText,-Math.PI/2);
    var z = this.createZ(minV, maxV, step, this.rotateXText, -Math.PI / 2);
//    var xz = this.createXZ(minV, maxV, step);
    face.add(x);
    face.add(z);
//    face.add(xz);

   
    return face;
};

Axis2D.prototype.createYZFace = function (minV, maxV, step) {
    var face = new THREE.Object3D();

    var y = this.createY(minV, maxV, step, this.rotateYText, Math.PI / 2);
    var z = this.createZ(minV, maxV, step, this.rotateYText, Math.PI/2 );
//    var yz = this.createYZ(minV, maxV, step);
    face.add(y);
    face.add(z);
//    face.add(yz);
    return face;
};

Axis2D.prototype.rotateXText = function(text, angle) {
    text.rotateX(angle);
};

Axis2D.prototype.rotateYText = function (text, angle) {
    text.rotateY(angle);
};

Axis2D.prototype.rotateZText = function (text, angle) {
    text.rotateZ(angle);
};

Axis2D.prototype.createX = function (minV, maxV, step,rotateText,angle) {

    var group = new THREE.Object3D();
    var arrowX = new THREE.ArrowHelper(new THREE.Vector3(1, 0, 0), minV, (maxV.x - minV.x) + 3 * step, 0xB22222, 10, 3);
    group.add(arrowX);
    
    //添加x文字。

    var xText = new THREE.THREEDTextHelper({ text: 'x' }, this.geometries);
    xText.position.set(maxV.x + 3 * step, minV.y, minV.z);
    if (rotateText) {
        rotateText(xText, angle);
    }
   
//    xText.rotateX(-Math.PI / 2);
    group.add(xText);
    
    for (var i = minV.x; i <= maxV.x; i += step) {
        var value = new THREE.THREEDTextHelper({ text: i, size: scaleSize }, this.geometries);
        value.position.set(i, minV.y, minV.z);
        if (rotateText) {
            rotateText(value, angle);
        }
       
        group.add(value);
    }
    return group;
};
Axis2D.prototype.createY = function (minV, maxV, step, rotateText, angle) {
    var group = new THREE.Object3D();
    var arrowY = new THREE.ArrowHelper(new THREE.Vector3(0, 1, 0), minV, (maxV.y - minV.y) + 3 * step, 0x98F198, 10, 3);
    group.add(arrowY);
    
    //添加x文字。

    var xText = new THREE.THREEDTextHelper({ text: 'y' }, this.geometries);
    xText.position.set(minV.x, maxV.y + 3 * step, minV.z);
    if (rotateText) {
        rotateText(xText, angle);
    }
    group.add(xText);
    
    for (var i = minV.y; i <= maxV.y; i += step) {
        var value = new THREE.THREEDTextHelper({ text: i, size: scaleSize }, this.geometries);
        value.position.set(minV.x, i, minV.z);
        if (rotateText) {
            rotateText(value, angle);
        }
        group.add(value);
    }
    return group;
};

Axis2D.prototype.createZ = function (minV, maxV, step, rotateText, angle) {
    var group = new THREE.Object3D();
    var arrowZ = new THREE.ArrowHelper(new THREE.Vector3(0, 0, 1), minV, (maxV.z - minV.z) + 3 * step, 0x7B68EE, 10, 3);
    group.add(arrowZ);
    
    //添加x文字。

    var xText = new THREE.THREEDTextHelper({ text: 'z' }, this.geometries);
    xText.position.set(minV.x, minV.y, maxV.z + 3 * step);
    if (rotateText) {
        rotateText(xText, angle);
    }
    group.add(xText);
    
    for (var i = minV.z; i <= maxV.z; i += step) {
        var value = new THREE.THREEDTextHelper({ text: i, size: scaleSize }, this.geometries);
        value.position.set(minV.x, minV.y, i);
        if (rotateText) {
            rotateText(value, angle);
        }
        group.add(value);
    }
    return group;
};


Axis2D.prototype.createXY = function(minV, maxV, step) {
    if (0.1) {
        var z1 = new THREE.RectangleHelper(maxV.x, maxV.y, minV.x, minV.y, step, 0.1);
        z1.rotation.x -= Math.PI / 2;
//        z1.rotation.z -= Math.PI / 2;

    }
    return z1;
};

Axis2D.prototype.createXZ = function (minV, maxV, step) {
    if (0.1) {
        var z1 = new THREE.RectangleHelper(maxV.x, maxV.z, minV.x, minV.z, step, 0.1);
//        z1.rotation.x -= Math.PI / 2;
//        z1.rotation.z -= Math.PI / 2;

        return z1;
    }
};

Axis2D.prototype.createYZ = function (minV, maxV, step) {
    if (0.1) {
        var z1 = new THREE.RectangleHelper(maxV.y, maxV.z, minV.y, minV.z, step, 0.1);
//        z1.rotation.x -= Math.PI / 2;
        z1.rotation.z += Math.PI / 2;

        return z1;
    }
}